package com.exartisan.network;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.exartisan.bean.Album;
import com.exartisan.bean.Answer;
import com.exartisan.bean.Event;
import com.exartisan.bean.GalleryContent;
import com.exartisan.bean.Post;
import com.exartisan.bean.Quiz;
import com.exartisan.bean.Survey;

public class NewResponseTranslater {

	public static boolean checkSuccess(String jsonResponse) {
		JSONObject jsonObject;
		boolean isSuccess = false;
		try {
			jsonObject = new JSONObject(jsonResponse);
			if (jsonObject.getString(NewNetworkUtility.HANDLE).equals(
					NewNetworkUtility.ON_SYSTEM_ERROR)||jsonObject.has("error"))
				isSuccess = false;
			else{
				isSuccess = true;
			}
		} catch (JSONException e) {
		}
		return isSuccess;
	}

	public static String getHandle(String jsonResponse) {
		JSONObject jsonObject;
		String handle = "";
		try {
			jsonObject = new JSONObject(jsonResponse);
			handle = jsonObject.getString(NewNetworkUtility.HANDLE);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return handle;
	}

	public static String getErrorMessage(String jsonResponse) {
		JSONObject jsonObject;
		String msg = "";
		try {
			jsonObject = new JSONObject(jsonResponse);
			msg = jsonObject.getString(NewNetworkUtility.ERROR_MESSAGE);
		} catch (JSONException e) {

		}
		return msg;
	}

	// public static ArrayList<Event> getTopEvent(String jsonResponse)
	// {
	// ArrayList<Event> alEvents = new ArrayList<Event>();
	// long eventID;
	// int type; // 0: top event | 1: new event
	// String eventName;
	// String startDate;
	// String endDate;
	// String thumbUrl;
	// String fbFeedID = null;
	// try
	// {
	// JSONObject jsonRp = new JSONObject(jsonResponse);
	// JSONObject jsonQuestion = jsonRp
	// .getJSONObject(NewNetworkUtility.QUESTION);
	// JSONArray jsonTable = jsonQuestion
	// .getJSONArray(NewNetworkUtility.TABLE);
	// for (int i = 0; i < jsonTable.length(); i++)
	// {
	// JSONObject jsonElement = jsonTable.getJSONObject(i);
	// eventID = jsonElement.getLong(NewNetworkUtility.ID);
	// type = 0;
	// eventName = jsonElement.getString(NewNetworkUtility.EVENTNAME);
	// startDate = jsonElement.getString(NewNetworkUtility.STARTDATE);
	// endDate = jsonElement.getString(NewNetworkUtility.ENDDATE);
	// thumbUrl = jsonElement.getString(NewNetworkUtility.THUMBURL);
	// Event event = new Event(eventID, type, eventName, startDate,
	// endDate, thumbUrl, fbFeedID);
	// event.setArticle(jsonElement.getString(NewNetworkUtility.ARTICLE));
	// event.setDescription(jsonElement
	// .getString(NewNetworkUtility.DESCRIPTION));
	// event.setCoverUrl(jsonElement
	// .getString(NewNetworkUtility.THUMBURL));
	// event.setFbFeedID(jsonElement.getString(NewNetworkUtility.FEEDID));
	// alEvents.add(event);
	// }
	// }
	// catch (JSONException e)
	// {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return alEvents;
	// }

	// public static ArrayList<Event> getNewEvent(String jsonResponse)
	// {
	// ArrayList<Event> alEvents = new ArrayList<Event>();
	// long eventID;
	// int type; // 0: top event | 1: new event
	// String eventName;
	// String startDate;
	// String endDate;
	// String thumbUrl;
	// String description;
	// String fbFeedID = null;
	// try
	// {
	// JSONObject jsonRp = new JSONObject(jsonResponse);
	// JSONObject jsonQuestion = jsonRp
	// .getJSONObject(NewNetworkUtility.QUESTION);
	// JSONArray jsonTable = jsonQuestion
	// .getJSONArray(NewNetworkUtility.TABLE);
	// for (int i = 0; i < jsonTable.length(); i++)
	// {
	// JSONObject jsonElement = jsonTable.getJSONObject(i);
	// eventID = jsonElement.getLong(NewNetworkUtility.ID);
	// type = 0;
	// eventName = jsonElement.getString(NewNetworkUtility.EVENTNAME);
	// startDate = jsonElement.getString(NewNetworkUtility.STARTDATE);
	// endDate = jsonElement.getString(NewNetworkUtility.ENDDATE);
	// thumbUrl = jsonElement.getString(NewNetworkUtility.THUMBURL);
	// description = jsonElement.getString(NewNetworkUtility.DESCRIPTION);
	// Event event = new Event(eventID, type, eventName, startDate,
	// endDate, thumbUrl, description, fbFeedID);
	// event.setArticle(jsonElement.getString(NewNetworkUtility.ARTICLE));
	// event.setCoverUrl(jsonElement
	// .getString(NewNetworkUtility.THUMBURL));
	// event.setFbFeedID(jsonElement.getString(NewNetworkUtility.FEEDID));
	// alEvents.add(event);
	// }
	// }
	// catch (JSONException e)
	// {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return alEvents;
	// }

	// public static ArrayList<Gallery> getGalleryInfo(String jsonResponse)
	// {
	// ArrayList<Gallery> galleries = new ArrayList<Gallery>();
	// long galleryID;
	// long eventID;
	// String eventName;
	// String thumbURL;
	// try
	// {
	// JSONObject jsonRp = new JSONObject(jsonResponse);
	// JSONObject jsonQuestion = jsonRp
	// .getJSONObject(NewNetworkUtility.QUESTION);
	// JSONArray jsonTable = jsonQuestion
	// .getJSONArray(NewNetworkUtility.TABLE);
	// for (int i = 0; i < jsonTable.length(); i++)
	// {
	// JSONObject jsonElement = jsonTable.getJSONObject(i);
	// galleryID = jsonElement.getLong(NewNetworkUtility.ID);
	// eventID = jsonElement.getLong(NewNetworkUtility.EVENTID);
	// eventName = jsonElement.getString(NewNetworkUtility.EVENTNAME2);
	// thumbURL = jsonElement.getString(NewNetworkUtility.MEDIA_URL);
	// Gallery gallery = new Gallery(galleryID, eventID, eventName,
	// thumbURL);
	// galleries.add(gallery);
	// }
	// }
	// catch (JSONException e)
	// {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return galleries;
	// }
	//
	// public static ArrayList<GalleryContent> getEventGallery(String
	// jsonResponse)
	// {
	// ArrayList<GalleryContent> contents = new ArrayList<GalleryContent>();
	// long contentID;
	// String resourceUrl;
	// String description;
	// int type;
	// try
	// {
	// JSONObject jsonRp = new JSONObject(jsonResponse);
	// JSONObject jsonQuestion = jsonRp
	// .getJSONObject(NewNetworkUtility.QUESTION);
	// JSONArray jsonTable = jsonQuestion
	// .getJSONArray(NewNetworkUtility.TABLE);
	// for (int i = 0; i < jsonTable.length(); i++)
	// {
	// JSONObject jsonElement = jsonTable.getJSONObject(i);
	// contentID = jsonElement.getLong(NewNetworkUtility.ID);
	// resourceUrl = jsonElement.getString(NewNetworkUtility.MEDIA_URL);
	// type = jsonElement.getInt(NewNetworkUtility.TYPE);
	// description = jsonElement.getString(NewNetworkUtility.MEDIA_TITLE);
	// GalleryContent content = new GalleryContent(contentID,
	// resourceUrl, description, type);
	// contents.add(content);
	// }
	// }
	// catch (JSONException e)
	// {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return contents;
	// }

	// public static Event getDetailEvent(String jsonResponse)
	// {
	// Event event = new Event();
	// String startDate;
	// String endDate;
	// String description;
	// String article;
	// String place;
	// String coverUrl;
	// try
	// {
	// JSONObject jsonRp = new JSONObject(jsonResponse);
	// JSONObject jsonQuestion = jsonRp
	// .getJSONObject(NewNetworkUtility.QUESTION);
	// JSONObject jsonTable = jsonQuestion
	// .getJSONObject(NewNetworkUtility.TABLE);
	// event.setEventID(jsonTable.getLong(NewNetworkUtility.ID));
	// event.setEventName(jsonTable.getString(NewNetworkUtility.EVENTNAME));
	// event.setStartDate(jsonTable.getString(NewNetworkUtility.STARTDATE));
	// event.setCoverUrl(jsonTable.getString(NewNetworkUtility.THUMBURL));
	// event.setEndDate(jsonTable.getString(NewNetworkUtility.ENDDATE));
	// event.setPlace(jsonTable.getString(NewNetworkUtility.PLACE));
	// event.setArticle(jsonTable.getString(NewNetworkUtility.ARTICLE));
	// }
	// catch (JSONException e)
	// {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return event;
	// }

	/**
	 * Parser data envent
	 * 
	 * @param jsonEvent
	 * @return
	 */
	public static ArrayList<Event> getEvent(String jsonEvent) {
		ArrayList<Event> alEvent = new ArrayList<Event>();
		int EventID;
		String EventName;
		String FeedID;
		String DateCreate;
		String DateStart;
		String DateEnd;
		String Image;
		String ImageThumb;
		String EventContent;
		String EventAddress;
		try {
			JSONObject event_JsonObject = new JSONObject(jsonEvent);
			JSONObject jsonQuestion = event_JsonObject.getJSONObject("events");
			JSONArray jsonTable = jsonQuestion
					.getJSONArray(NewNetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				//
				EventID = jsonElement.getInt(NewNetworkUtility.EVENTID);
				EventName = jsonElement
						.getString(NewNetworkUtility.EVENT_TITLE);
				// FeedID = jsonElement.getString(NewNetworkUtility.FEEDID);
				FeedID = jsonElement
						.getString(NewNetworkUtility.FEEDID);;
				DateCreate = jsonElement
						.getString(NewNetworkUtility.TIME_CREATE);
				DateStart = jsonElement.getString(NewNetworkUtility.DATE_START);
				DateEnd = jsonElement.getString(NewNetworkUtility.DATE_END);
				Image = jsonElement.getString(NewNetworkUtility.IMG);
				ImageThumb = jsonElement.getString(NewNetworkUtility.IMG_THUMB);
				EventContent = jsonElement
						.getString(NewNetworkUtility.EVENT_CONTENT);
				EventAddress = jsonElement
						.getString(NewNetworkUtility.EVENT_ADDRESS);
				Event event = new Event(EventID, EventName, DateStart, DateEnd,
						ImageThumb, FeedID, EventAddress, EventContent, Image);
				event.setDescription(jsonElement.getString("EventDescription"));
				event.setViewCount(jsonElement.getInt("Views"));
				alEvent.add(event);
				//

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alEvent;
	}

	/**
	 * parser data posts
	 * 
	 * @param jsonPost
	 * @return
	 */
	public static ArrayList<Post> getPosts(String jsonPost) {
		ArrayList<Post> alPost = new ArrayList<Post>();
		int PostID;
		int CategoryID;
		int PostType;
		String PostDescription;
		String PostContent;
		String DateCreate;
		int UserCreate;
		String Image;
		String ImageThumb;
		String FeedID;
		try {
			JSONObject event_JsonObject = new JSONObject(jsonPost);
			JSONObject jsonQuestion = event_JsonObject
					.getJSONObject("category");
			JSONArray jsonTable = jsonQuestion
					.getJSONArray(NewNetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				//
				PostID = jsonElement.getInt(NewNetworkUtility.POST_ID);
				CategoryID = 1; // jsonElement.getInt(NewNetworkUtility.CATEGORY_ID);
				PostType = 1; // jsonElement.getInt(NewNetworkUtility.POST_TYPE);
				PostDescription = jsonElement
						.getString(NewNetworkUtility.POST_DESCRIPTION);
				PostContent = jsonElement
						.getString(NewNetworkUtility.POST_CONTENT);
				DateCreate = jsonElement.getString("DateCreate");
				UserCreate = 2; // jsonElement.getInt(NewNetworkUtility.USER_CREATE);
				Image = jsonElement.getString(NewNetworkUtility.IMG);
				ImageThumb = jsonElement.getString(NewNetworkUtility.IMG_THUMB);
				FeedID = jsonElement.getString(NewNetworkUtility.FEEDID);
				Post post = new Post(PostID, CategoryID, PostType,
						PostDescription, PostContent, DateCreate, UserCreate
								+ "", FeedID, Image, ImageThumb);
				post.setTitle(jsonElement
						.getString(NewNetworkUtility.POST_TITLE));
				post.setUserName(jsonElement
						.getString(NewNetworkUtility.POST_USER_NAME));
				post.setUserAvatarUrl(jsonElement
						.getString(NewNetworkUtility.POST_USER_AVATAR_URL));
				post.setViews(jsonElement.getInt("Views"));
				alPost.add(post);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alPost;
	}

	public static ArrayList<Album> getAlbum(String jsonAlbum) {
		ArrayList<Album> alAlbum = new ArrayList<Album>();
		try {
			JSONArray jsonTable = new JSONObject(jsonAlbum).getJSONObject(
					"medias").getJSONArray(NewNetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				Album album = new Album();

				album.setMediaTitlte(jsonElement.getString("MediaTitlte"));
				album.setMediaID(jsonElement.getInt("MediaID"));
				album.setFullName(jsonElement.getString("FullName"));
				album.setAvatar(jsonElement.getString("Avatar"));
				album.setDateCreate(jsonElement.getString("DateCreate"));
				album.setViews(jsonElement.getInt("Views"));
				album.setImage(jsonElement.getString(NewNetworkUtility.IMG));
				album.setImageThumb(jsonElement
						.getString(NewNetworkUtility.IMG_THUMB));
				album.setFbFeedID(jsonElement.getString(NewNetworkUtility.FEEDID));

				alAlbum.add(album);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return alAlbum;

	}

	/**
	 * parser data gallery
	 * 
	 * @param jsonGallery
	 * @return
	 */
	public static ArrayList<GalleryContent> getDetailGallery(String jsonGallery) {
		ArrayList<GalleryContent> alContent = new ArrayList<GalleryContent>();
		int MediaID;
		String MediaTitle;
		String MediaDescription;
		int MediaType;
		String MediaLink;
		String Image;
		String ImageThumb;
		try {
			JSONObject gallerryJsonObject = new JSONObject(jsonGallery);
			JSONObject jsonQuestion = gallerryJsonObject.getJSONObject("Media");
			JSONArray jsonTable = jsonQuestion
					.getJSONArray(NewNetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				//
				MediaID = jsonElement.getInt("MediaDetailID");
				MediaTitle = jsonElement.getString("MediaDetailTitle");
				MediaDescription = ""; // jsonElement.getString(NewNetworkUtility.MEDIA_DESCRIPTION);
				MediaType = jsonElement.getInt(NewNetworkUtility.MEDIA_TYPE);
				MediaLink = jsonElement.getString(NewNetworkUtility.MEDIA_LINK);
				// Image = jsonElement.getString(NewNetworkUtility.IMG);
				// ImageThumb =
				// jsonElement.getString(NewNetworkUtility.IMG_THUMB);
				GalleryContent content = new GalleryContent(MediaID, MediaLink,
						MediaDescription, MediaTitle, MediaType);
				content.setFeedID(jsonElement
						.getString(NewNetworkUtility.FEEDID));
				content.setDateCreate(jsonElement
						.getString(NewNetworkUtility.DATE_CREATE));
				content.setFullName(jsonElement.getString("FullName"));
				content.setAvatar(jsonElement.getString("Avatar"));
				alContent.add(content);
				//
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alContent;
	}

	public static ArrayList<GalleryContent> getEventDetailGallery(
			String jsonGallery) {
		ArrayList<GalleryContent> alContent = new ArrayList<GalleryContent>();
		int MediaID;
		String MediaTitle;
		String MediaDescription;
		int MediaType;
		String MediaLink;
		String Image;
		String ImageThumb;
		try {
			JSONObject gallerryJsonObject = new JSONObject(jsonGallery);
			JSONObject jsonQuestion = gallerryJsonObject.getJSONObject("Media");
			JSONArray jsonTable = jsonQuestion
					.getJSONArray(NewNetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				//
				MediaID = jsonElement.getInt("MediaID");
				MediaTitle = jsonElement.getString("MediaTitle");
				MediaDescription = ""; // jsonElement.getString(NewNetworkUtility.MEDIA_DESCRIPTION);
				MediaType = jsonElement.getInt(NewNetworkUtility.MEDIA_TYPE);
				// MediaLink =
				// jsonElement.getString(NewNetworkUtility.MEDIA_LINK);
				Image = jsonElement.getString(NewNetworkUtility.IMG);
				ImageThumb = jsonElement.getString(NewNetworkUtility.IMG_THUMB);
				GalleryContent content = new GalleryContent(MediaID, Image,
						MediaDescription, MediaTitle, MediaType);
				content.setFeedID(jsonElement
						.getString(NewNetworkUtility.FEEDID));
				content.setDateCreate(jsonElement
						.getString(NewNetworkUtility.DATE_CREATE));
				content.setFullName(jsonElement.getString("FullName"));
				content.setAvatar(jsonElement.getString("Avatar"));
				alContent.add(content);
				//
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alContent;
	}

	/**
	 * parser data Servey
	 * 
	 * @param jsonSurvey
	 * @return
	 */
	public static ArrayList<Survey> getSurvey(String jsonSurvey) {
		ArrayList<Survey> alSurvey = new ArrayList<Survey>();
		int SurveyID;
		String SurveyName;
		int EventID;
		String DateCreate;
		String DateStart;
		String DateEnd;
		String Image;
		String ImageThumb;
		int status;
		String description;

		try {
			JSONObject servey_JsonObject = new JSONObject(jsonSurvey);
			JSONObject jsonQuestion = servey_JsonObject
					.getJSONObject("surveys");
			JSONArray jsonArrayTable = jsonQuestion
					.getJSONArray(NewNetworkUtility.TABLE);
			for (int i = 0; i < jsonArrayTable.length(); i++) {
				JSONObject elementJsonObject = jsonArrayTable.getJSONObject(i);
				//
				SurveyID = elementJsonObject
						.getInt(NewNetworkUtility.SURVEY_ID);
				SurveyName = elementJsonObject.getString("SurveyTitle");
				EventID = elementJsonObject.getInt(NewNetworkUtility.EVENTID);
				DateCreate = ""; // elementJsonObject.getString(NewNetworkUtility.TIME_CREATE);
				DateStart = ""; // elementJsonObject.getString(NewNetworkUtility.DATE_START);
				DateEnd = ""; // elementJsonObject.getString(NewNetworkUtility.DATE_END);
				Image = elementJsonObject.getString(NewNetworkUtility.IMG);
				ImageThumb = elementJsonObject
						.getString(NewNetworkUtility.IMG_THUMB);
				status = elementJsonObject.getInt(NewNetworkUtility.STATUS);
				Survey survey = new Survey(SurveyID, SurveyName, DateCreate,
						DateStart, DateEnd, status);
				survey.setThumbUrl(ImageThumb);
				survey.setJoin(elementJsonObject.getInt("Joins"));
				survey.setDescription(elementJsonObject.getString("SurveyDescription"));
				alSurvey.add(survey);
				//
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alSurvey;
	}

	/**
	 * parser data Questions
	 * 
	 * @param jsonSurvey
	 * @return
	 */
	public static ArrayList<Quiz> getQuestion(String jsonQuestions) {
		ArrayList<Quiz> alQuiz = new ArrayList<Quiz>();
		int QuestionID;
		String QuestionName;
		int QuestionType;
		int SurveyID;
		ArrayList<Answer> alAnswer;

		try {
			JSONObject question_JsonObject = new JSONObject(jsonQuestions);
			JSONObject jsonQuestion = question_JsonObject
					.getJSONObject("Media");
			JSONArray jsonArrayTable = jsonQuestion
					.getJSONArray(NewNetworkUtility.TABLE);
			for (int i = 0; i < jsonArrayTable.length(); i++) {
				alAnswer = new ArrayList<Answer>();
				JSONObject elementJsonObject = jsonArrayTable.getJSONObject(i);
				//
				QuestionID = elementJsonObject
						.getInt(NewNetworkUtility.QUESTION_ID);
				QuestionName = elementJsonObject
						.getString(NewNetworkUtility.QUESTION_NAME);
				QuestionType = elementJsonObject
						.getInt(NewNetworkUtility.QUESTION_TYPE);
				SurveyID = elementJsonObject
						.getInt(NewNetworkUtility.SURVEY_ID);
				JSONArray jsonAnswer = elementJsonObject.getJSONArray("ListAnswer");
				if (jsonAnswer.length()<1) continue;
				for (int j = 0; j < jsonAnswer.length(); j++) {
					JSONObject jsonElement = jsonAnswer.getJSONObject(j);
					alAnswer.add(new Answer(jsonElement.getInt("AnswerID"), jsonElement.getString("AnswerTitle")));
				}
				Quiz quiz = new Quiz(QuestionID, QuestionName, QuestionType, alAnswer);
				alQuiz.add(quiz);
				//
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alQuiz;
	}
	
	public static ArrayList<GalleryContent> getEventGallery(String jsonGallery) {
		ArrayList<GalleryContent> alContent = new ArrayList<GalleryContent>();
		int MediaID;
		String MediaTitle;
		String MediaDescription;
		int MediaType;
		String MediaLink;
		String Image;
		String ImageThumb;
		try {
			JSONObject gallerryJsonObject = new JSONObject(jsonGallery);
			JSONObject jsonQuestion = gallerryJsonObject.getJSONObject("Media");
			JSONArray jsonTable = jsonQuestion
					.getJSONArray(NewNetworkUtility.TABLE);
			for (int i = 0; i < jsonTable.length(); i++) {
				JSONObject jsonElement = jsonTable.getJSONObject(i);
				//
				MediaID = jsonElement.getInt("MediaID");
				MediaTitle = jsonElement.getString("MediaTitle");
				MediaDescription = ""; // jsonElement.getString(NewNetworkUtility.MEDIA_DESCRIPTION);
				//MediaType = jsonElement.getInt(NewNetworkUtility.MEDIA_TYPE);
				MediaType=1;
				MediaLink = jsonElement.getString("Image");
				// Image = jsonElement.getString(NewNetworkUtility.IMG);
				// ImageThumb =
				// jsonElement.getString(NewNetworkUtility.IMG_THUMB);
				GalleryContent content = new GalleryContent(MediaID, MediaLink,
						MediaDescription, MediaTitle, MediaType);
				content.setFeedID(jsonElement
						.getString(NewNetworkUtility.FEEDID));
				content.setDateCreate(jsonElement
						.getString(NewNetworkUtility.DATE_CREATE));
				content.setFullName(jsonElement.getString("FullName"));
				content.setAvatar(jsonElement.getString("Avatar"));
				alContent.add(content);
				//
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return alContent;
	}
	
	
}